var LayerUtil = 
{
	createLayer : function( innerHTML )
	{
		return new FantasyLayer( innerHTML );	
	}
}

var FantasyLayer = function ( innerHTML )
{
	var IE = document.all ? true : false ;
	
	var iid = "myfantasylayer";
	
	var layerElement;
	
	if(document.getElementById("myfantasylayer")){
	  layerElement = document.getElementById("myfantasylayer");
	}else{
	  layerElement = document.createElement("DIV");
	  layerElement.id = iid;
	}
	
	var layerInnerHTML	= innerHTML;
	
	this.closeLayer = function()
	{
		layerElement.innerHTML = "&nbsp;";
		layerElement.style.display = "none";
	}
	
	this.getLayer = function ()
	{
		return layerElement;
	}
	
	this.setInnerHTML = function ( html )
	{
		layerInnerHTML = html;
	}
	
	this.setDisplay = function( isView )
	{
		this.reloadLayer();
		layerElement.style.display = isView ? "block" : "none";
	}
	
	this.moveLayer = function ( x , y )
	{
		layerElement.style.left = x + "px";
		layerElement.style.top =  y + "px";
	}

	this.viewLayer = function ( isFade , x , y )
	{
		this.reloadLayer( x , y );
		if( !isFade ) { return false };
		IE ? layerElement.style.filter = "alpha(opacity=0)" : layerElement.style.opacity = "0";
		var opacity = 0 ;
		var intervalID = window.setInterval
		(
			function()
			{
				if( opacity < 100 )
				{ 
					if( IE )
					{
						layerElement.style.filter = "alpha(opacity=" +  opacity + ")";
					}
					else
					{
						layerElement.style.opacity = (opacity / 100);
					}
					opacity = opacity + 5;
				}
				else
				{ 
					window.clearInterval( intervalID );
				}
			}
			,
			50
		)
	}

	this.reloadLayer = function ( x , y )
	{
		//document.body.appendChild(layerElement);
		layerElement.style.position = "absolute";
		layerElement.style.display = "block";
		layerElement.style.border = "1px";
		layerElement.innerHTML = layerInnerHTML;
		var docWidth = document.documentElement.clientWidth;
		var docHeight = document.documentElement.clientHeight;
		var scrollTop = document.documentElement.scrollTop;
		var scrollLeft = document.documentElement.scrollLeft;
		//layerElement.style.left = ( !isNaN(x) ? x : (docWidth - layerElement.offsetWidth)/2   + scrollLeft)   + "px";
		//layerElement.style.top  = ( !isNaN(y) ? y : (docHeight - layerElement.offsetHeight)/2 + scrollTop )   + "px";
		
		event.srcElement.parentNode.insertBefore(layerElement,event.srcElement.nextSibling);
	}
}